package com.yy.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.yy.bean.Group;
import com.yy.bean.Hero;
import com.yy.mydota.R;
import com.yy.widget.MyGridView;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyexpandableListAdapter extends BaseExpandableListAdapter {
	private Context mContext;
	private LayoutInflater inflater;
	private List<Group> groupList;
	private List<List<Hero>> childList;
	private MyGridViewAdapter myGridViewAdapter;

	public MyexpandableListAdapter(Context context, List<Group> groupList,
			List<List<Hero>> childList) {
		this.mContext = context;
		inflater = LayoutInflater.from(context);
		this.groupList = groupList;
		this.childList = childList;
	}
	
	
	class GroupHolder {
		TextView textView;
		ImageView imageView;
	}

	class ChildHolder {
		MyGridView gridview_hero;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return childList.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildHolder childHolder = null;
		if (convertView == null) {
			childHolder = new ChildHolder();
			convertView = inflater.inflate(R.layout.item_gridview, null);
			childHolder.gridview_hero = (MyGridView) convertView.findViewById(R.id.gridview_hero);
			
            convertView.setTag(childHolder);
		}else{
			childHolder = (ChildHolder) convertView.getTag();
		}
		final List<Hero> heros = childList.get(groupPosition);
		
		myGridViewAdapter = new MyGridViewAdapter(mContext, heros);
		childHolder.gridview_hero.setAdapter(myGridViewAdapter);
		childHolder.gridview_hero.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long arg3) {
				String name = heros.get(position).getName();
				Toast.makeText(mContext, "pic" + (position+1)+":"+name, Toast.LENGTH_SHORT).show();
			}
		});
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groupList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return groupList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		GroupHolder groupHolder = null;
		if (convertView == null) {
			groupHolder = new GroupHolder();
			convertView = inflater.inflate(R.layout.item_gridview_tag, null);
			groupHolder.textView = (TextView) convertView
                    .findViewById(R.id.texv_gridvTag);
            convertView.setTag(groupHolder);
		}else{
			groupHolder = (GroupHolder) convertView.getTag();
		}
		
		groupHolder.textView.setText(((Group) getGroup(groupPosition)).getTitle());
		
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO 
		return true;
	}

}
